package cn.demomaster.quickui_sample;

import static cn.demomaster.quicklogger.QdLog.DEFAULT_TAG;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;
import android.util.Log;


//import com.didichuxing.doraemonkit.DoraemonKit;

import com.demomaster.quickcompat.content.QuickSharedPreferences;
import com.demomaster.quickcompat.util.QDFileUtil;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Member;
import java.security.InvalidParameterException;

//import android_serialport_api.SerialPort;
//import android_serialport_api.SerialPortFinder;
import android_serialport_api2.SerialPort;
import android_serialport_api2.SerialPortFinder;
import cn.demomaster.quickdb.QuickDb;
import cn.demomaster.quicklogger.QdLog;
import cn.demomaster.quicklogger.config.LogConfigBuilder;
import cn.demomaster.quickpermission.QuickPermission;
import cn.demomaster.quickui.QuickApplication;
import cn.demomaster.quickui.helper.SoundHelper;
import cn.demomaster.quickui.helper.cache.QuickCache;

public class Application extends QuickApplication {
    QuickDb quickDb;

    @Override
    public void onCreate() {
        super.onCreate();
        LogConfigBuilder configBuilder = new LogConfigBuilder(this);
        configBuilder.setSaveExternalStorageBeforeAndroidQ(false);
        configBuilder.setSaveInternalSoragePath("/log/");
        configBuilder.setSaveExternalStoragePath(new File(Environment.getExternalStorageDirectory(),"QdLog/log"));
        QdLog.init(this, configBuilder.build());

        //QuickPermission.getInstance().init(this);
        quickDb = new QuickDb(this, "quickdev.db", null, 10);
        try {
            dbHelper.createTable(Member.class);
            dbHelper.updateTable(Member.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        SoundHelper.getInstance().init(this, true, R.raw.class);//自动加载raw下的音频文件
        //初始化友盟分享
        //initUmengShare("5c79138f61f564e0380012fa");
       /* UMConfigure.init(this,"5c79138f61f564e0380012fa"
                ,"umeng", UMConfigure.DEVICE_TYPE_PHONE,"");//58edcfeb310c93091c000be2 5965ee00734be40b580001a0

        PlatformConfig.setWeixin("wxdc1e388c3822c80b", "3baf1193c85774b3fd9d18447d76cab0");
        //豆瓣RENREN平台目前只能在服务器端配置
        PlatformConfig.setSinaWeibo("3921700954", "04b48b094faeb16683c32669824ebdad","http://sns.whalecloud.com");
        PlatformConfig.setYixin("yxc0614e80c9304c11b0391514d09f13bf");
        PlatformConfig.setQQZone("100424468", "c7394704798a158208a74ab60104f0ba");
        PlatformConfig.setTwitter("3aIN7fuF685MuZ7jtXkQxalyi", "MK6FEYG63eWcpDFgRYw4w9puJhzDl0tyuqWjZ3M7XJuuG7mMbO");
        PlatformConfig.setAlipay("2015111700822536");
        PlatformConfig.setLaiwang("laiwangd497e70d4", "d497e70d4c3e4efeab1381476bac4c5e");
        PlatformConfig.setPinterest("1439206");
        PlatformConfig.setKakao("e4f60e065048eb031e235c806b31c70f");
        PlatformConfig.setDing("dingoalmlnohc0wggfedpk");
        PlatformConfig.setVKontakte("5764965","5My6SNliAaLxEm3Lyd9J");
        PlatformConfig.setDropbox("oz8v5apet3arcdy","h7p2pjbzkkxt02a");*/
        
        //DoraemonKit.install(this);
        //初始化缓存目录
        //QuickCache.init(this, Environment.getExternalStorageDirectory().getAbsolutePath()+"/QdLog/cache/");
        QuickCache.getInstance().init(this, QDFileUtil.getDiskCacheDir(getInstance()) +"/cache/");

        //若想使用无障碍服务 需要完成初始化实现类 AccessibilityServiceManager.getInstance().init();
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        Log.i(DEFAULT_TAG, "attachBaseContext");
        //MultiDex.install(base);
    }

    @Override
    public QuickDb getDbHelper() {
        if (dbHelper == null) {
            dbHelper = new QuickDb(this, "quick_db.db", "quick_db.db", null, 10, this);
            //quick_db1.db 在data/data/下生成对应的db文件
            //quick_db2.db 在assets下的db文件
            try {
                dbHelper.createTable(Member.class);
                dbHelper.updateTable(Member.class);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return dbHelper;
    }

    public final SerialPortFinder mSerialPortFinder = new SerialPortFinder();
    private SerialPort mSerialPort = null;

    public SerialPort getSerialPort() throws SecurityException, IOException, InvalidParameterException {
        if (mSerialPort == null) {
            /* Read serial port parameters */
            //SharedPreferences sp = getSharedPreferences("android_serialport_api.sample_preferences", MODE_PRIVATE);//"cn_demomaster_huan_quickdevelop_fragment_helper_serialport_sample_SerialPortPreferences"
            SharedPreferences sp = QuickSharedPreferences.getInstance().getSharedPreferences();
            String path = sp.getString("DEVICE", "");
            int baudrate = Integer.decode(sp.getString("BAUDRATE", "-1"));

            /* Check parameters */
            if ((path.length() == 0) || (baudrate == -1)) {
                throw new InvalidParameterException();
            }

            Log.d("getSerialPort", path + ":" + baudrate);
            /* Open the serial port */
            mSerialPort = new SerialPort(new File(path), baudrate, 0);
        }
        return mSerialPort;
    }

    public void closeSerialPort() {
        if (mSerialPort != null) {
            mSerialPort.close();
            mSerialPort = null;
        }
    }
}
